import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xin_he/page/adopt/xin_he_adopt_certificate/xin_he_adopt_certificate_view.dart';
import 'package:xin_he/page/login/xin_he_login_action.dart';
import 'package:xin_he/page/mine/xin_he_my_qr_code/xin_he_my_qr_code_view.dart';
import 'package:xin_he/utils/xh_styles.dart';
import 'package:xin_he/utils/xin_he_image.dart';
import 'package:xin_he/utils/xin_he_toast.dart';
import 'package:xin_he/utils/xin_he_user_data.dart';

import '../../../routes/xin_he_routes.dart';
import '../../login/xin_he_login/xin_he_login_view.dart';
import '../../login/xin_he_register/xin_he_register_view.dart';
import 'xin_he_mine_logic.dart';

class XinHeMinePage extends StatefulWidget {
  const XinHeMinePage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _XinHeMinePageState();
}

class _XinHeMinePageState extends  State<XinHeMinePage> {

  final logic = Get.put(XinHeMineLogic());
  final state = Get.find<XinHeMineLogic>().state;

  @override
  Widget build(BuildContext context) {

    return GetBuilder<XinHeMineLogic>(builder: (context){
      return Obx(() => Scaffold(
        resizeToAvoidBottomInset: false,
        body: Stack(children: [
          //
          XinHeAssetImage('assets/images/mine/header_bg.png',width: Get.width,height: Get.width*327.5/375,),

          ListView(
            // physics: const NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            padding: EdgeInsets.zero,
            children: [
              SizedBox(height: 44+Get.mediaQuery.padding.top,),
              ///头像
              Row(children: [

                GestureDetector(
                  onTap: (){
                    if(xinHeUserData.appLoginState.value == false){
                      XinHeLoginAction.modalToLoginPage();
                      return;
                    }
                    Get.toNamed(XinHeRoutes.editProfile);
                  },
                  child: Row(children: [
                    const SizedBox(width: 15,),
                    ///头像
                    Container(
                      width: 51,
                      height: 51,
                      alignment: Alignment.center,
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(25.5),
                        child: xinHeUserData.appLoginState.value? XinHeNetImage(url: xinHeUserData.userDetailInfo.value!.iconPath??'',width: 51,height: 51,fit: BoxFit.cover,): const XinHeAssetImage('assets/images/mine/default_avatar.png',width: 51,height: 51,),

                      ),
                    ),
                    const SizedBox(width: 10,),
                    ///姓名
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(xinHeUserData.appLoginState.value?xinHeUserData.userDetailInfo.value!.name??'':'请登录',style: TextStyle(color: XinHeColor('#222222'),fontSize: 15,fontWeight: FontWeight.w600),),
                        const SizedBox(height: 6,),
                        GestureDetector(
                            onTap: (){
                              if(xinHeUserData.appLoginState.value == false){
                                XinHeLoginAction.modalToLoginPage();
                                return;
                              }
                              Get.dialog(XinHeAdoptCertificatePage());
                            },
                            child: const XinHeAssetImage('assets/images/mine/vip.png',width: 60,height: 18,)),

                      ],),
                  ],),
                ),

                const Spacer(),
                GestureDetector(
                    onTap: (){
                      if(xinHeUserData.appLoginState.value == false){
                        XinHeLoginAction.modalToLoginPage();
                        return;
                      }
                      Get.dialog(XinHeMyQrCodePage());
                    },
                    child: const XinHeAssetImage('assets/images/mine/mine_qrcode.png',width: 26,height: 26,)),
                const SizedBox(width: 25,),



              ],),

              _topTabsView(),

              Container(
                margin: const EdgeInsets.symmetric(horizontal: 15),
                padding: const EdgeInsets.symmetric(horizontal: 15),
                decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(10) ),
                child: Column(children: [

                  SizedBox(height: 15,),
                  Row(children: [
                    const Text('我的订单',style: TextStyle(color: Colors.black,fontSize: 14,fontWeight: FontWeight.w600),),

                    Spacer(),

                    GestureDetector(
                      onTap: (){
                        if(xinHeUserData.appLoginState.value == false){
                          XinHeLoginAction.modalToLoginPage();
                          return;
                        }
                        Get.toNamed(XinHeRoutes.myOrders,arguments: 0);

                      },
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Text('全部订单',style: TextStyle(color: XinHeColor('#AAAAAA'),fontSize: 12,fontWeight: FontWeight.w500),),
                          const XinHeAssetImage('assets/images/mine/mine_arrow.png',width: 6,height: 10,)
                        ],
                      ),
                    )
                  ],),
                  const SizedBox(height: 15,),
                  _orderTabsView(),
                  const SizedBox(height: 15,),


                ],),),

              const SizedBox(height: 10,),


              Container(
                margin: const EdgeInsets.symmetric(horizontal: 15),
                padding: const EdgeInsets.symmetric(horizontal: 15),
                decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(10) ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [

                    const SizedBox(height: 15,),
                    const Text('常用功能',style: TextStyle(color: Colors.black,fontSize: 14,fontWeight: FontWeight.w600),),
                    const SizedBox(height: 10,),
                    _funcItemsView(),
                    const SizedBox(height: 10,),


                  ],),),

              xinHeUserData.appLoginState.value? GestureDetector(
                onTap: (){
                  if(xinHeUserData.appLoginState.value){
                    logic.loginOut();
                  }else {
                    XinHeLoginAction.modalToLoginPage();

                  }
                },
                child: Container(
                  margin: const EdgeInsets.only(top: 17,left: 130, right: 130),
                  height: 34,
                  // width: 105,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(borderRadius: BorderRadius.circular(17),border: Border.all(color: XinHeColor('#BECAC9'),width: 0.5)),
                  child: Text(xinHeUserData.appLoginState.value?'退出登录':'请登录',style: TextStyle(color: XinHeColor('#637171'),fontSize: 13),),),
              ):Container()

            ],)

        ],),));
    });
  }



  Widget _topTabsView() {
    return Container(
      width: Get.width,
      height: 76,
      // color: Colors.red,
      child: GridView.builder(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        padding: const EdgeInsets.symmetric(horizontal: 0),
        itemCount: logic.topTabList.length,
        gridDelegate:  SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: logic.topTabList.length,
          mainAxisSpacing: 22,
          crossAxisSpacing: 0,
          // childAspectRatio: 163.5/200.0
        ),
        itemBuilder: (ctx,index){

          var sss = logic.topTabValueList[index];
          return GestureDetector(
            onTap: (){
              if(index == 0){
                Get.toNamed(XinHeRoutes.myCloudChaZi);

              }
              if(index == 1){
                Get.toNamed(XinHeRoutes.oilCard);

              }
              if(index == 2){
                Get.toNamed(XinHeRoutes.castFeeRedbag);

              }
              if(index == 3){
                Get.toNamed(XinHeRoutes.leftMoney);

              }
            },
            behavior: HitTestBehavior.opaque,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [

                Text( xinHeUserData.appLoginState.value ?'$sss':'0.0',style: TextStyle(color: XinHeColor('#033333'),fontSize: 16),),
                SizedBox(height: 5,),
                Container(
                  alignment: Alignment.center,
                  height: 22.5,
                  child: Text(logic.topTabList[index],style: TextStyle(fontSize: 13,color: XinHeColor('#2B3131'),fontWeight: FontWeight.w500)),
                ),
                const SizedBox(height: 15,)
              ],),
          );
        },
      ),
    );
  }


  Widget _orderTabsView() {
    return Container(
      width: Get.width-30,
      height: 48,
      child: GridView.builder(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        padding: const EdgeInsets.symmetric(horizontal: 0),
        itemCount: logic.orderTabList.length,
        gridDelegate:  SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: logic.orderTabList.length,
          mainAxisSpacing: 22,
          crossAxisSpacing: 0,
          // childAspectRatio: 163.5/200.0
        ),
        itemBuilder: (ctx,index){
          return GestureDetector(
            onTap: (){
              if(index == 4){
                Get.toNamed(XinHeRoutes.applySaleAfterList,arguments: index);

                return;
              }

              Get.toNamed(XinHeRoutes.myOrders,arguments: index+1);
            },
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [

                XinHeAssetImage('assets/images/mine/${logic.orderTabIcon[index]}.png',width: 27,height: 27,),

                SizedBox(height: 1,),

                Container(
                  alignment: Alignment.center,
                  child: Text(logic.orderTabList[index],style: TextStyle(fontSize: 13,color: XinHeColor('#2B3131'),fontWeight: FontWeight.w500)),
                ),


                const SizedBox(height: 15,)
              ],),
          );
        },
      ),
    );
  }

  Widget _funcItemsView() {
    return Container(
      width: Get.width-30,
      alignment: Alignment.center,
      child: GridView.builder(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        padding: const EdgeInsets.symmetric(horizontal: 0,vertical: 0),
        itemCount: logic.funcTitleList.length,
        gridDelegate:  const SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 3,
          mainAxisSpacing: 0,
          crossAxisSpacing: 15,
          // childAspectRatio: 163.5/200.0
        ),
        itemBuilder: (ctx,index){
          return GestureDetector(
            onTap: (){

              if(!xinHeUserData.appLoginState.value){
                Get.bottomSheet(XinHeRegisterPage(),persistent: false,isScrollControlled: true);

                // XinHeLoginAction.modalToLoginPage();
                return;
              }

              var timeNumber = DateTime.now().millisecondsSinceEpoch;
              String st = "$timeNumber";
              // Get.to(XinHeCategoryPage(tag: st,),arguments: index,preventDuplicates: false);

              if(index == 0){
                Get.toNamed(XinHeRoutes.realNameAuth);
              }

              if(index == 1){
                Get.toNamed(XinHeRoutes.myTeam);
              }
              if(index == 2){
                Get.toNamed(XinHeRoutes.myCoupons);
              }
              if(index == 3){
                Get.toNamed(XinHeRoutes.myFavor);
              }

              if(index == 4){
                Get.toNamed(XinHeRoutes.inviteFriend);
              }
              if(index == 5){
                Get.toNamed(XinHeRoutes.workSpace);
              }
              if(index == 6 ){
                Get.toNamed(XinHeRoutes.bankManagement);

                // XinHeToast.center('正在开发，敬请期待');
              }
              if(index == 7){
                Get.toNamed(XinHeRoutes.addressManagement);
              }
              if(index == 8){
                XinHeToast.center('正在开发，敬请期待');
              }
            },
            child: Container(
              color: Colors.transparent,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [

                  XinHeAssetImage('assets/images/mine/${logic.funcTitleIcon[index]}.png',width: 26,height: 24,),

                  const SizedBox(height: 5,),

                  Container(
                    alignment: Alignment.center,
                    child: Text(logic.funcTitleList[index],style: TextStyle(fontSize: 13,color: XinHeColor('#2B3131'),fontWeight: FontWeight.w500)),
                  ),


                ],),
            ),
          );
        },
      ),
    );
  }


  // @override
  // void didChangeDependencies() {
  //   // TODO: implement didChangeDependencies
  //   super.didChangeDependencies();
  // }
  //
  // @override
  // void dispose() {
  //   // TODO: implement dispose
  //   super.dispose();
  // }



}
